﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GSdk.Net.Lglcd;

namespace GSdk.Net.HelloWorld.Advanced
{
    public partial class HelloWorldForm : Form
    {
        // Important part: BW image must be 160x43, while QVGA must be 320x240. Both must have Format32bppArgb as format
        Bitmap formImageBW = new Bitmap(160, 43, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        Bitmap formImageQVGA = new Bitmap(320, 240, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        Applet app;
        public HelloWorldForm()
        {
            InitializeComponent();

            // Create the applet with wanted parameters
            app = new Applet()
            {
                Name = Text,
                Autostartable = false,
                SupportedDevices = DeviceTypes.BlackAndWhite | DeviceTypes.Qvga,
            };

            // Connect to applet
            app.Connect();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this, "Test message", "this is a test", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void HelloWorldForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Closing operations, we can also call Dispose only (it will Disconnect automatically)
            app.Disconnect();
            app.Dispose();
        }

        private void UpdateGraphics()
        {
            // Because we want allow the possibility to change the device collection totally, Devices variable is of type DeviceCollection
            (app.Devices as DefaultDeviceCollection).BlackAndWhite.Update(formImageBW);
            // If you don't like casting, this is an utility method that does it for you
            app.GetDevices<DefaultDeviceCollection>().Qvga.Update(formImageQVGA);
        }

        private void HelloWorldForm_Load(object sender, EventArgs e)
        {
            // Some graphic operations
            using (Graphics g = Graphics.FromImage(formImageBW))
            {
                // Use only black or white brushes to write on a black and white bitmap image
                // Only the R of the color is used when converting to the byte array required for Lglcd update, to reduce CPU consumption
                g.FillRectangle(Brushes.White, new Rectangle(0, 0, 160, 43));
                g.DrawString("ciao sono francesco", Font, Brushes.Black, new RectangleF(0f, 0f, 159f, 42f));
            }

            DrawToBitmap(formImageQVGA, new Rectangle(0, 0, 320, 240));

            // Let's update devices
            UpdateGraphics();

            // Some winform operations
            pictureBox1.Image = formImageQVGA;
            pictureBox2.Image = formImageBW;
        }
    }
}
